package org.exmaple.springframework.beans.factory.support;

import cn.hutool.core.bean.BeanUtil;
import org.exmaple.springframework.beans.BeansException;
import org.exmaple.springframework.beans.PropertyValue;
import org.exmaple.springframework.beans.PropertyValues;
import org.exmaple.springframework.beans.factory.config.BeanDefinition;
import org.exmaple.springframework.beans.factory.config.BeanReference;

import java.lang.reflect.Constructor;
import java.util.Objects;

/**
 * @author DeYou
 * @date 2022/10/27
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory{

    private InstantiationStrategy instantiationStrategy = new CglibSubclassingInstantiationStrategy();

    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    /**
     * 提供接口修改实例化策略
     * @param instantiationStrategy
     */
    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }

    @Override
    protected Object createBean(BeanDefinition beanDefinition,String beanName,Object[] args) throws BeansException {
        Object bean;
        try{
            bean = createBeanInstance(beanDefinition,beanName,args);
            //给bean 对象填充属性
            applyPropertyValues(beanName,bean,beanDefinition);

        }


        catch (Exception e){
            throw new BeansException("Instantiation of bean failed", e);
        }
        //注册到单例子bean 容器
        addSingleton( beanName,bean );
        return bean;
    }

    protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
        try{
            PropertyValues propertyValues =beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValues()){
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();
                if(value instanceof BeanReference){
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean( beanReference.getBeanName() );
                }
                BeanUtil.setFieldValue( bean,name,value );
            }
        }catch (Exception e) {
            throw new BeansException("Error setting property values：" + beanName);
        }
    }

    protected Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
        //获取要使用哪个构造方法实例化。
        Constructor constructorToUse = null;
        Class<?> beanClass = beanDefinition.getBeanClass();
        Constructor<?>[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor<?> ctor : declaredConstructors) {
            if(Objects.nonNull(args) && ctor.getParameterTypes().length == args.length){
                constructorToUse = ctor;
                break;
            }
        }

        return getInstantiationStrategy().instantiate( beanDefinition,beanName,constructorToUse,args );
    }
}